package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SubVideoMonitorModelAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@AttrHandler(SubVideoMonitorModelAttrHandler.class)
public class SubVideoMonitorModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 处理结算价、销售价、产品公允价
        this.dealPrice(attr, inst, data);

        // 型号联动物资编码
        this.goodsNbrAttrLinkHandle(attr, inst, data);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void goodsNbrAttrLinkHandle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 物资编码
        KiteAttr goodsNbrAttr = inst.getAttr("200004178");
        if (Objects.nonNull(goodsNbrAttr)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(goodsNbrAttr.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, goodsNbrAttr.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
    }

    private void dealPrice(KiteAttr attr, KiteInst inst, AttrData data) {
        // 产品公允价
        KiteAttr commonPrice = inst.getAttr("200004115");
        // 销售价
        KiteAttr salesPrice = inst.getAttr("200004107");
        // 结算价
        KiteAttr settlePrice = inst.getAttr("200004108");
        if (Objects.nonNull(commonPrice)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(commonPrice.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, commonPrice.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
        if (Objects.nonNull(salesPrice)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(salesPrice.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, salesPrice.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
        if (Objects.nonNull(settlePrice)) {
            String attrValueSpec = AttrValueCache.getAttrValueByAttrValueCode(settlePrice.getAttrId(), attr.getValue());
            operateInstAttrService.setAttrValue(inst, settlePrice.getFieldName(), attrValueSpec, data.getSceneInstId(), null);
        }
    }
}
